home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCTXT.ZIP / CHAP14.TXT < prev    next >
Text File  |  1987-11-21  |  14KB  |  323 lines

  1.  
  2.                      Chapter 14 - Example Programs
  3.  
  4.  
  5.                              WHY THIS CHAPTER?
  6.  
  7.              Although  every  program  in this tutorial has  been  a
  8.         complete  program,  each  one  has also been  a  very  small
  9.         program intended to teach you some principle of  programming
  10.         in  C.   It  would do you a disservice to leave you at  that
  11.         point  without introducing you to a few larger  programs  to
  12.         illustrate  how  to  put together the  constructs  you  have
  13.         learned  to create a major program.   This chapter  contains
  14.         four  programs  of increasing complexity,  each designed  to
  15.         take  you  into a higher plateau of  programming,  and  each
  16.         designed to be useful to you in some way.
  17.  
  18.              DOSEX will illustrate how to make DOS system calls  and
  19.         will teach you,  through self-study, how the system responds
  20.         to  the  keyboard.   WHATNEXT  reads commands input  on  the
  21.         command line and will aid you in setting up a variable batch
  22.         file,  one  that requests an operator input and responds  to
  23.         the  input  by branching to a different part  of  the  batch
  24.         file.
  25.  
  26.              LIST  is  the source code for the program you  used  to
  27.         print  out the C source files when you began studying C with
  28.         the aid of this tutorial.  Finally we come to VC, the Visual
  29.         Calculator,  which  you should find to be a  useful  program
  30.         even  if you don't study its source code.   VC uses most  of
  31.         the  programming  techniques we have studied in this  course
  32.         and  a few that we never even mentioned such  as  separately
  33.         compiled subroutines.
  34.  
  35.              We  will  take a look at the example programs one at  a
  36.         time  but  without  a complete explanation of  any  of  them
  37.         because  you  have  been studying C for some  time  now  and
  38.         should be able to read and understand most of these programs
  39.         on  your  own.
  40.  
  41.                      DOSEX.C - The DOS Example Program
  42.  
  43.              The  copy of DOS that you received with your IBM-PC  or
  44.         compatible has about 80 internal DOS calls that you can  use
  45.         as  a programmer to control your peripheral devices and read
  46.         information  or status from them.   Some of the earlier  IBM
  47.         DOS manuals, DOS 2.0 and earlier, have these calls listed in
  48.         the back of the manual along with how to use them.   Most of
  49.         the  manuals  supplied  with compatible  computers  make  no
  50.         mention  of  these  calls even  though  they  are  extremely
  51.         useful.   These  calls  can  be  accessed  from  nearly  any
  52.         programming  language but they do require some initial study
  53.         to learn how to use them.   This program is intended to  aid
  54.         you in this study.
  55.  
  56.  
  57.  
  58.                                   Page 99
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      Chapter 14 - Example Programs
  69.  
  70.  
  71.              Display the program on your monitor or print it out for
  72.         reference.   It  is  merely a loop watching for  a  keyboard
  73.         input or a change in the time.  If either happens, it reacts
  74.         accordingly.   In line 32,  the function "kbhit()" returns a
  75.         value  of 1 if a key has been hit but not yet read from  the
  76.         input buffer by the program.
  77.  
  78.              Look at the function named "get_time" for an example of
  79.         a  DOS call.   An interrupt 21(hex) is called after  setting
  80.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is
  81.         returned in the CH,  CL, and DH registers.  Refer to the DOS
  82.         call  definitions in your copy of DOS.   If the  definitions
  83.         are  not included there,  Peter Nortons  book,  "Programmers
  84.         Guide  to  the  IBM PC" is recommended as a  good  reference
  85.         manual   for   these  calls  and  many   other   programming
  86.         techniques.   Your compiler may have a built in function  to
  87.         do this.  If you read your documentation, you will  probably
  88.         find many useful functions available with your compiler that
  89.         are  included  as  a convenience for you  by  your  compiler
  90.         writer.
  91.  
  92.              Another useful function is the "pos_cursor()"  function
  93.         that  positions the cursor anywhere on the monitor that  you
  94.         desire  by  using  a  DOS  interrupt.   In  this  case,  the
  95.         interrupt  used  is  10(hex) which is  the  general  monitor
  96.         interrupt.  This particular service is number 2 of about  10
  97.         different  monitor  services available.   This  function  is
  98.         included here as another example to you.
  99.  
  100.              The  next  function,  service  number  6  of  interrupt
  101.         10(hex)  is the window scroll service.   It should  be  self
  102.         explanatory.
  103.  
  104.              In this program, the cursor is positioned and some data
  105.         is  output  to the monitor,  then the cursor is "hidden"  by
  106.         moving  it  to line 26 which is not  displayed.   After  you
  107.         compile and run the program, you will notice that the cursor
  108.         is  not  visible on the monitor.   This is possible  in  any
  109.         program,  but  be  sure  to put the cursor  in  view  before
  110.         returning  to  DOS  because  DOS does not  like  to  have  a
  111.         "hidden" cursor and may do some strange things.
  112.  
  113.              Some time spent studying this program will be  valuable
  114.         to  you as it will reveal how the keyboard data is input  to
  115.         the  computer.   Especially of importance is how the special
  116.         keys such as function keys, arrows, etc. are handled.   Also
  117.         note that this program uses full prototype checking and is a
  118.         good  example  of  how to use it.  Since it  also  uses  the
  119.         "modern"  method  of  function definitions,  it  is  a  good
  120.         example of that also.
  121.  
  122.  
  123.  
  124.                                   Page 100
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                      Chapter 14 - Example Programs
  135.  
  136.  
  137.  
  138.                   WHATNEXT.C - The Batch File Interrogator
  139.  
  140.              This  is  an  example of how to read the  data  on  the
  141.         command line following the function call.  Notice that there
  142.         are  two variables listed within the  parentheses  following
  143.         the main() call.   The first variable is a count of words in
  144.         the entire command line including the command itself and the
  145.         second  variable  is  a  pointer to  an  array  of  pointers
  146.         defining the actual words on the command line.
  147.  
  148.              First the question on the command line, made up of some
  149.         number of words, is displayed on the monitor and the program
  150.         waits for the operator to hit a key.   If the key hit is one
  151.         of  those  in the last "word" of the group of words  on  the
  152.         command  line,  the number of the character within the group
  153.         is  returned to the program where it can be tested with  the
  154.         "errorlevel" command in the batch file.   You could use this
  155.         technique  to  create a variable AUTOEXEC.BAT  file  or  any
  156.         other  batch  file  can  use this for  a  many  way  branch.
  157.         Compile  and  run this file with TEST.BAT for an example  of
  158.         how  it  works in practice.   You may  find  this  technique
  159.         useful  in  one  of  your batch files and  you  will  almost
  160.         certainly  need  to  read in  the  command  line  parameters
  161.         someday.
  162.  
  163.              An  interesting alternative would be for you to write a
  164.         program  named "WOULD.C" that would return a 1 if a  "Y"  or
  165.         "y"  were typed and a zero if any other key were hit.   Then
  166.         your batch file could have a line such as;
  167.  
  168.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  169.  
  170.              Dos would use "WOULD" as the program name,  ignore  the
  171.         rest  of  the  statement  except for displaying  it  on  the
  172.         screen.   You  would  then respond to the  question  on  the
  173.         monitor  with a single keyhit.   Your batch file would  then
  174.         respond   to  the  1  or  0  returned  and  either  run  the
  175.         alternative  part  of  the batch file or  the  primary  part
  176.         whatever each part was.
  177.  
  178.         WOULD YOU LIKE PRIMARY (Y/N)
  179.         IF ERRORLEVEL 1 GOTO PRIMARY
  180.         (secondary commands)
  181.         GOTO DONE
  182.         :PRIMARY
  183.         (primary commands)
  184.         :DONE
  185.  
  186.  
  187.  
  188.  
  189.  
  190.                                   Page 101
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                      Chapter 14 - Example Programs
  201.  
  202.  
  203.  
  204.                         LIST.C - The Program Lister
  205.  
  206.              This program is actually composed of two files,  LIST.C
  207.         and  LISTF.C  that must be separately  compiled  and  linked
  208.         together  with your linker.   There is nothing new here  and
  209.         you  should  have  no  trouble compiling  and  linking  this
  210.         program  by  reading the documentation  supplied  with  your
  211.         C compiler.
  212.  
  213.              The  only  thing  that is new in this  program  is  the
  214.         inclusion   of  three  "extern"  variables  in  the  LISTF.C
  215.         listing.   The only purpose for this is to tie these  global
  216.         variables  to  the main program and tell the  compiler  that
  217.         these  are not new variables.   The compiler will  therefore
  218.         not  generate any new storage space for them but simply  use
  219.         their names during the compile process.   At link time,  the
  220.         linker  will  get  their actual storage locations  from  the
  221.         LIST.OBJ  file and use those locations for the variables  in
  222.         the  LISTF part of the memory map also.   The  variables  of
  223.         those  names in both files are therefore the same  identical
  224.         variables and can be used just as any other global variables
  225.         could be used if both parts of the program were in one file.
  226.  
  227.              There is no reason why the variables couldn't have been
  228.         defined  in the LISTF.C part of the program and declared  as
  229.         "extern"  in the LIST.C part.   Some of the variables  could
  230.         have  been  defined  in one and some in the  other.   It  is
  231.         merely a matter of personal taste.   Carried to an  extreme,
  232.         all of the variables could have been defined in a third file
  233.         and  named "extern" in both of these files.   The third file
  234.         would then be compiled and included in the linking process.
  235.  
  236.              It would be to your advantage to compile, link, and run
  237.         this  program to prepare you for the next program  which  is
  238.         composed of 6 separate files which must all work together.
  239.  
  240.                         VC.C - The Visual Calculator
  241.  
  242.              This  program  finally ties nearly everything  together
  243.         because  it uses nearly every concept covered in the  entire
  244.         tutorial.   It  is so big that I will not even try to  cover
  245.         the finer points of its operation.   Only a few of the  more
  246.         important points will be discussed.
  247.  
  248.              The  first  thing  you  should do  is  go  through  the
  249.         tutorial  for  VC included in the file  VC.DOC.   There  are
  250.         several  dozen  steps  for you to execute,  with  each  step
  251.         illustrating some aspect of the Visual Calculator.  You will
  252.         get  a  good feel for what it is capable of doing  and  make
  253.         your study of the source code very profitable.  In addition,
  254.  
  255.  
  256.                                   Page 102
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                      Chapter 14 - Example Programs
  267.  
  268.  
  269.         you  will  probably  find  many  ways  to  use  the   Visual
  270.         Calculator  to  solve problems involving calculations  where
  271.         the  simplicity  of  the problem at hand  does  not  warrant
  272.         writing a program.
  273.  
  274.              Notice that the structure definitions,  used in all  of
  275.         the  separate parts of the program,  are defined in the file
  276.         STRUCT.DEF.   During  program development,  when  it  became
  277.         necessary  to change one of the structures slightly,  it was
  278.         not  necessary to change it in all of the  files,  only  one
  279.         file  required modification which was then "included" in the
  280.         source files.   Notice that the transcript data is stored in
  281.         a doubly linked list with the data itself being stored in  a
  282.         separate  dynamically allocated char string.   This line  is
  283.         pointed to by the pointer "lineloc".
  284.  
  285.              For  ease  of development,  the similar functions  were
  286.         grouped together and compiled separately.   Thus, all of the
  287.         functions  involving the monitor were included in  the  file
  288.         named  VIDEO.C,  and all of the functions involving the data
  289.         storage were grouped into the FILE.C  collection.   Dividing
  290.         your  program  in  a  way similar to  this  should  simplify
  291.         debugging and future modifications.
  292.  
  293.              Of special interest is the "monitor()" function.   This
  294.         function  examines  the  video mode through  use  of  a  DOS
  295.         command  and  if it is a 7,  it assumes it is  a  monochrome
  296.         monitor,  otherwise it assumes a color monitor.   The colors
  297.         of  the various fields are established at this time and used
  298.         throughout  the  program.   Most  of  the  data  is  written
  299.         directly  to the video memory,  but some is written  through
  300.         the standard BIOS routines.
  301.  
  302.              The file DEFIN.H is a catalogue of the functions to aid
  303.         in finding the functions.  This file was generated as one of
  304.         the  first  files  and was maintained and  updated  for  use
  305.         during  the  entire  design and coding  lifetime.   It  also
  306.         contains all of the prototype definitions for the  functions
  307.         in  all  of  the source files, and is  "included"  in  every
  308.         source file to do prototype checking.
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.                                   Page 103
  323.